package kr.ac.jejuuniv.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.ac.jejuuniv.model.User;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor{

	public void postHandle(HttpServletRequest req, HttpServletResponse res, Object handler, ModelAndView mav) throws Exception {
		
	}

	public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {
		if(handler.getClass() == LoginController.class)
			return true;
		User user = (User) req.getSession().getAttribute("user");
		if(user == null) {
			String redirectUrl = "/login?returnUrl=" + req.getRequestURI();
			res.sendRedirect(redirectUrl); 
			return false;
		}
		return true;
	}

	public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object handler, Exception e) throws Exception {
		
	}

}
